package com.xxdemo;

import io.minio.MinioClient;
import okhttp3.OkHttpClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;
import java.time.temporal.ChronoUnit;

@Configuration
public class GlobalConfig {
    @Bean
    public MinioClient minioClient() {
        String accessKey = "foobar";
        String secretKey = "P@ssw0rd";
        String endpointHost = "192.168.1.12";
        int endpointPort = 9000;
        Duration maxConnectTimeout = Duration.of(500, ChronoUnit.MILLIS);// TCP握手阶段耗时: 毫秒级
        Duration maxReadTimeout = Duration.of(15, ChronoUnit.SECONDS); // 业务数据收发延迟: 允许等待若干秒甚至几分钟
        Duration maxWriteTimeout = Duration.of(30, ChronoUnit.SECONDS); // 业务数据收发延迟: 同上
        OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder()
                .connectTimeout(maxConnectTimeout)
                .readTimeout(maxReadTimeout)
                .writeTimeout(maxWriteTimeout);
        OkHttpClient customizedOkHttpClient = okHttpClientBuilder.build();

        return MinioClient.builder()
                .httpClient(customizedOkHttpClient)
                .endpoint(endpointHost, endpointPort, false)
                .credentials(accessKey, secretKey)
                .build();
    }
}
